﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.IO;

namespace OS_PROJECT
{
    class Driver
    {
        // 设置虚拟设备及内存
        NewProcessQueue newProcessQueue = new NewProcessQueue();
        public NewProcessQueue NewProcessQueue
        { get { return newProcessQueue; } }

        WaitingQueue waitingQueue = new WaitingQueue();
        public WaitingQueue WaitingQueue
        { get { return waitingQueue; } }

        ReadyQueue readyQueue = new ReadyQueue();
        public ReadyQueue ReadyQueue
        { get { return readyQueue; } }

        public List<Process> deadProcesses = new List<Process>();

        Disk disk = new Disk();
        public Disk Disk
        { get { return disk; } }

        RAM memory = new RAM();
        public RAM RAM
        { get { return memory; } }

        Dispatcher dispatcher;
        public Dispatcher Dispatcher
        { get { return dispatcher; } }

        // 主程序初始化
       public Loader loader;
       public LongTermScheduler LTS;

        int numberOfCPUs = 4;
       public List<CPU> cpuList;

        bool shouldRun = true;

        public string url{get;set;}

        public Driver(string url)
        { this.url = url;

        MMU.Instantiate(this);
        loader = new Loader(this);
        LTS = new LongTermScheduler(this);
        dispatcher = new Dispatcher(this);
        cpuList = new List<CPU>();
        for (int i = 0; i < numberOfCPUs; i++)
        {
            cpuList.Add(new CPU(this, i));
        }
        
        }

        public void RunOS()
        {
            InterruptHandler.Start();
           

         

            // 主程序
            loader.Run();
            LTS.Run();

            int cpu = 0;

            while (shouldRun)
            {
                if (ReadyQueue.AccessQueue.Count == 0)
                {
                    shouldRun = false;
                }
                if (cpuList[cpu].isActive == true)  
                {
                    // 不做事情
                }
                else
                {
                    // 重新激活CPU
                    cpuList[cpu].ResumeCPU();
                }
                cpu++;
                cpu %= numberOfCPUs; 
            }

            bool wait = true;
            while (wait)
            {
                int finished = 0;
                foreach (CPU c in cpuList)
                {
                    if (!c.isActive)
                    {
                        finished++;
                    }
                }
                if (finished == numberOfCPUs)
                {
                    wait = false;
                }
            }
         //   SystemCaller.DiskDump(this);
       //     SystemCaller.ProcessCmpltWaitTimesDump(this);
        }

        void RunCPUs()
        {
            for (int i = 0; i < numberOfCPUs; i++)
            {
                cpuList[i].RunCPU();
            }
        }

        void ResumeCPUs()
        {
            for (int i = 0; i < numberOfCPUs; i++)
            {
                cpuList[i].ResumeCPU();
            }
        }
    }
}
